*** Results for Table 2
*** Dyadic regressions

clear all
set mem 100m

local path0="C:\Dropbox\GeneticsProject (1)\REStat\round_accepted_replication_files\"
local path1="C:\Dropbox\GeneticsProject (1)\REStat\round_accepted_replication_files\outregs"

cd "`path0'workfiles\"

set more off
use completed_data, clear

	* create settler mortality with the African data corrected based on Albouy
	capture drop logmort3
	gen logmort3=logmort2
	replace logmort3=logmort if countrycode=="SGP" | countrycode=="AUS"
	
	gen lnUKdist=log(geo_UK_distw)
	
	capture drop ln_eiu_ppm
	gen ln_eiu_ppm=log(eiu_ppm)	
	
	keep if idv~=.
	gsort -idv

	egen tt=fill(1(1)10)
	

*==============================================================================
*			Prepare data for dyadic regression
*==============================================================================

tempname 100 
postfile `100' str20 country1 str20 country2  ///
					income1 income2 ///
					idv1 idv2 ///
					bloodA1 bloodA2 ///
					bloodB1 bloodB2 ///
					hjtfp1 hjtfp2 ///
					ln_eiu_ppm1 ln_eiu_ppm2 ///
					continent1 continent2 ///
					///
			using "diadic_regressions.dta", replace every(1)


forvalues i=1(1)99 {
	local i1=`i'+1
	forvalues j=`i1'(1)100 {
		local country1=countrycode[`i']
		local country2=countrycode[`j']
		
		local income1=pwt_ln_rgdpwok[`i']
		local income2=pwt_ln_rgdpwok[`j']
		
		local idv1=idv[`i']
		local idv2=idv[`j']
		
		local bloodA1=bloodA[`i']
		local bloodA2=bloodA[`j']

		local bloodB1=bloodB[`i']
		local bloodB2=bloodB[`j']						
		
		local hjtfp1=hjtfp[`i']
		local hjtfp2=hjtfp[`j']	
		
		
		local ln_eiu_ppm1=ln_eiu_ppm[`i']
		local ln_eiu_ppm2=ln_eiu_ppm[`j']	
		
		local continent1=continent[`i']
		local continent2=continent[`j']	
		
		
		post `100' ("`country1'") ("`country2'")  ///
					(`income1') (`income2') ///
					(`idv1') (`idv2') ///
					(`bloodA1') (`bloodA2') ///
					(`bloodB1') (`bloodB2') ///
					(`hjtfp1') (`hjtfp2') ///
					(`ln_eiu_ppm1') (`ln_eiu_ppm2') ///
					(`continent1') (`continent2') 
					
	}
}

postclose `100'
	
		
		

*==============================================================
*					Prepare distance data
*==============================================================
use "`path0'Data\Geography\dist_cepii.dta", clear
cd "`path0'workfiles"

rename iso_o country1 
rename iso_d country2
replace country1="GER" if country1=="DEU"
replace country2="GER" if country2=="DEU"

compress
save distance_full_info, replace


*==============================================================
*					Prepare diadic variables
*==============================================================
use "`path0'workfiles\diadic_regressions.dta", clear
capture drop _merge
joinby country1 country2 using distance_full_info, unmatched(master)
tab _merge
drop _merge

gen ln_distw=log(distw)
gen ln_dist=log(dist)
gen ln_distcap=log(distcap)
gen ln_distwces=log(distwces)

 
*==============================================================
*					Create variables
*==============================================================

compress
gen dind= idv1- idv2
gen dy= income1- income2
gen dhjtfp= hjtfp1- hjtfp2
gen dpat= ln_eiu_ppm1- ln_eiu_ppm2

gen distE=( bloodA1- bloodA2)^2+( bloodB1- bloodB2)^2

matrix B=[323.5387, 48.161479 \	48.161479, 305.14265]
gen distM=sqrt(  (bloodA1-bloodA2)*(B[1,1]*(bloodA1-bloodA2) + B[2,1]*(bloodB1-bloodB2) ) + (bloodB1-bloodB2)*(B[1,2]*(bloodA1-bloodA2) + B[2,2]*(bloodB1-bloodB2) ) )
							  
quiet foreach var1 in ///		
		AGO ALB  ARE ARG AUS AUT BEL BFA BGD BGR BRA BTN CAN ///
		CHE CHL CHN COL CRI CZE DNK DOM ECU EGY ESP EST ETH FIN ///
		FJI FRA GBR GER GHA GRC GTM HND HRV HUN IDN IND IRL IRN ///
		IRQ ISL ISR ITA JAM JOR JPN KEN KOR KWT LBN LBY LKA LTU LUX ///
		LVA MAR MEX MLT MOZ MWI MYS NAM NGA NLD NOR NPL NZL PAK PAN ///
		PER PHL POL PRT ROM RUS SAU SCG SEN SLE SLV SVK SVN SWE ///
		SYR THA TTO TUR TWN TZA UKR URY USA VEN VNM ZAF ZMB  ///
		{
		
		*noisily di "`var1'"
		gen du_`var1'=(country1=="`var1'" | country2=="`var1'" )
		compress
}
		
*** Generate BRITISH dummy
gen dummy_british=0
quiet foreach var1 in USA AUS GBR NLD CAN NZL ZAF  {
	noisily di "`var1'"
    replace dummy_british=1 if (country1=="`var1'" | country2=="`var1'" ) 
    compress
}

*** Generate continent dummies

quiet foreach var1 in 1 2 3 4  {
	noisily di "`var1'"
    gen dummy_cont_`var1'=(continent1==`var1' | continent2==`var1' ) 
    compress
}

*** An alternative variable for clustering
gen cc=0
local counter=0
*** Generate country dummies
quiet foreach var1 in 	///
		AGO ALB  ARE ARG AUS AUT BEL BFA BGD BGR BRA BTN CAN ///
		CHE CHL CHN COL CRI CZE DNK DOM ECU EGY ESP EST ETH FIN ///
		FJI FRA GBR GER GHA GRC GTM HND HRV HUN IDN IND IRL IRN ///
		IRQ ISL ISR ITA JAM JOR JPN KEN KOR KWT LBN LBY LKA LTU LUX ///
		LVA MAR MEX MLT MOZ MWI MYS NAM NGA NLD NOR NPL NZL PAK PAN ///
		PER PHL POL PRT ROM RUS SAU SCG SEN SLE SLV SVK SVN SWE ///
		SYR THA TTO TUR TWN TZA UKR URY USA VEN VNM ZAF ZMB  ///
		{
    
	*noisily di "`var1'"
    replace cc=`counter' if (country1=="`var1'" | country2=="`var1'" )
	local counter=`counter'+1
    compress
}


*==============================================================
*					Run regressions
*==============================================================
xi: ivreg2  dy (dind = distE), cluster(country1) ffirst
xi: ivreg2  dy (dind = distE) du_*, cluster(country1) ffirst
xi: ivreg2  dy (dind = distE) du_*   ln_distw, cluster(country1) ffirst
